
#include "StdAfx.h"
#include <Macro.h>
#include "ObjectList.h"
#include "Object.h"
#include "Player.h"


ObjectList::ObjectList()
{
}

ObjectList::~ObjectList()
{
	RemoveAll();
}

BOOL ObjectList::Add(Object *pObject)
{
	M_OBJECT_LIST_ITR	iObject = find(pObject->GetObjectKey());
	
	if ( iObject != end() )
	{
		return FALSE;
	}

	//(*this)[pObject->GetObjectKey()] = pObject;
	insert(M_OBJECT_LIST_PAIR(pObject->GetObjectKey(),pObject)); 
	
	return TRUE;
}

BOOL ObjectList::Remove(Object *pObject)
{
	SASSERT( pObject != NULL, "[ObjectList::Remove] pObject != NULL" );

	M_OBJECT_LIST_ITR	iObject = find( pObject->GetObjectKey() );

	if ( iObject != end() )
	{
		erase( iObject );
		return TRUE;
	}

	return FALSE;
}

Object * ObjectList::Remove(const DWORD dwObjKey)
{
	M_OBJECT_LIST_ITR	iObject = find( dwObjKey );

	if (iObject != end())
	{
		Object* pObject = iObject->second;
		erase( iObject );
		return pObject;
	}
	
	return NULL;
}

Object* ObjectList::Find(const DWORD dwObjKey)
{
	M_OBJECT_LIST_ITR	iObject = find( dwObjKey );

	if (iObject != end())
	{
		return iObject->second;
	}

	return NULL;
}

VOID ObjectList::RemoveAll()
{
	clear();	
}

VOID ObjectList::Update(DWORD dwElapsedTime)
{
    M_OBJECT_LIST_ITR iObject = begin();
	
	for ( ; iObject != end() ; iObject++ )
	{
		Object* pObject = iObject->second;
		pObject->Update( dwElapsedTime );
	}
}
